﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;
using System.IO;
using System.Reflection;
using System;

namespace ZOIL.DataBackend.Server
{
    /// <summary>
    /// Represents the Server. It holds References to all Databases an is visualized in the Server GUI
    /// </summary>
    public class Server
    {
        public String DatabaseDirectory { get; set; }

        private Server()
        {
            DatabaseDirectory = checkForDatabaseDir();

            Databases = new ObservableCollection<DatabaseConfiguration>();

            _databaseConfigs = Db4oFactory.OpenFile("config.db4o");

            IEnumerable<DatabaseConfiguration> configs = from DatabaseConfiguration config in _databaseConfigs
                                                         select config;

            foreach (DatabaseConfiguration config in configs)
            {
                //we need to activate the config, cause the DB4O Server could not be saved in the DB4O Database
                config.Activate();
                Databases.Add(config);
            }
        }

        private string checkForDatabaseDir()
        {
            var applicationPath = Assembly.GetExecutingAssembly().Location;
            var applicationDir = applicationPath.Substring(0, applicationPath.LastIndexOf('\\'));
            var databaseDir = applicationDir + "\\databases";
            var info = new DirectoryInfo(databaseDir);
            if (!info.Exists)
            {
                info.Create();
            }
            return databaseDir;
        }

        #region Singleton

        private static Server Instance;

        public static Server getInstance()
        {
            if (Instance == null)
            {
                Instance = new Server();
            }
            return Instance;
        }

        #endregion

        /// <summary>
        /// A local DB4O Container that persists all Databases we have created throughout the runtime of the application
        /// </summary>
        private IObjectContainer _databaseConfigs;

        /// <summary>
        /// saves the database configurations in the local config container
        /// </summary>
        public void SaveConfig()
        {
            foreach (DatabaseConfiguration config in Databases)
            {
                //we need to Deactivate the config, because the DB4O-Server can not be saved in the container
                config.Deactivate();
                _databaseConfigs.Store(config);
            }
        }

        /// <summary>
        /// closes the config container
        /// </summary>
        public void CloseConfigContainer()
        {
            _databaseConfigs.Close();
        }

        /// <summary>
        /// Removes the given config from the Database collection and the config container
        /// </summary>
        /// <param name="config"></param>
        public void Remove(DatabaseConfiguration config)
        {
            Databases.Remove(config);
            _databaseConfigs.Delete(config);
        }

        /// <summary>
        /// Collection of Databases that the server should manage
        /// </summary>
        public ObservableCollection<DatabaseConfiguration> Databases { get; private set; }

        
    }
}
